from common.login import Login
from common.report_generate import Report
import time,random
from faker import Factory
from selenium.webdriver.support.select import Select

#验证资源管理-培训资源模块，新增功能

class Add():
    def __init__(self):
        self.st = Login()
        self.driver = self.st.driver
        self.st.login_fast('超级管理员')
        self.st.open_module('资源管理', '培训资源')
        # 打开资源管理-培训资源模块
        self.st.Decrypt()


    #培训资源新增资源
    def add_pool(self):
        #生成随机电话，姓名
        fake = Factory().create('zh_CN')
        phone = fake.phone_number()
        name = fake.name()
        insert = Report()
        #使用超级管理员账号登录
        #self.st.login_fast('超级管理员')
        # #打开资源管理-培训资源模块

        #传值
        self.driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='搜索'])[1]/following::button[1]").click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[1]/div[1]/input').send_keys(phone)
        self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[1]/div[2]/input').send_keys(name)
        Select(self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[1]/div[3]/select')).select_by_index(1)
        Select(self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[2]/div[1]/select')).select_by_index(2)
        self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[2]/div[2]/input').send_keys(phone)
        Select(self.driver.find_element_by_xpath('//*[@id="addCus"]/div[1]/div[5]/div[1]/select')).select_by_index(3)
        self.driver.find_element_by_id('addCusBtn').click()
        time.sleep(2)
        #断言
        try:
            self.driver.find_element_by_xpath('/html/body/div[12]/div/div/div[3]/button').click()
            print('新增成功')
            insert.write_report('3.5','资源管理','GUI测试','TC-005','新增培训资源','成功','无','无')
            self.driver.quit()
            return name
        except:
            print('新增失败')
            img = insert.pic_screenshot()
            insert.write_report('3.5', '资源管理', 'GUI测试', 'TC-005', '新增培训资源', '失败', '无',img)

    #培训资源临时池修改资源
    def update(self):
        Select(self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/select[1]')).select_by_index(1)
        time.sleep(1)

        self.driver.find_element_by_xpath('//*[@id="personal-table"]/tbody/tr[1]/td[15]/button[2]').click()
        time.sleep(2)
        select_type = Select(self.driver.find_element_by_xpath('//*[@id="modifyForm"]/div/div[5]/div[2]/select'))

        #渠道来源随机下标
        select_options = select_type.options
        random_index = random.randint(1, len(select_options) - 1)
        select_type.select_by_index(random_index)
        text = select_options[random_index].text
        self.driver.find_element_by_xpath('//*[@id="alterCusBtn"]').click()

        #数据比对
        time.sleep(1)
        result = self.driver.find_element_by_xpath('//*[@id="personal-table"]/tbody/tr[1]/td[4]').text

        #断言
        insert = Report()
        if result == text:
            print('修改成功')
            insert.write_report('3.5', '资源管理', 'GUI测试', 'TC-006', '修改培训资源', '成功', '无', '无')
        else:
            print('修改失败')
            img = insert.pic_screenshot()
            insert.write_report('3.5', '资源管理', 'GUI测试', 'TC-006', '修改培训资源', '成功', '无', img)

    # 培训资源临时池跟踪资源
    def trak(self):
        #选择临时池
        Select(self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/select[1]')).select_by_index(1)
        time.sleep(1)
        #点击跟踪
        self.driver.find_element_by_xpath('//*[@id="personal-table"]/tbody/tr[10]/td[15]/button[1]').click()
        time.sleep(1)
        #选择跟踪资源
        self.driver.find_element_by_link_text('跟踪资源').click()
        time.sleep(1)

        #传值
        name = self.driver.find_element_by_id('customerName')
        Select(self.driver.find_element_by_xpath('//*[@id="newStatus"]')).select_by_index(1)
        Select(self.driver.find_element_by_xpath('//*[@id="formFollow"]/div[3]/select')).select_by_index(1)
        self.driver.find_element_by_xpath('/html/body/div[12]').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('/html/body/div[9]/div[3]/table/tbody/tr[3]/td[7]').click()
        self.driver.find_element_by_xpath('//*[@id="showTracking"]').send_keys('test')
        self.driver.find_element_by_xpath('//*[@id="saveTrackingBtn"]').click()

        #打开个人池对比数据
        Select(self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/select[1]')).select_by_index(2)
        time.sleep(1)
        result = self.driver.find_element_by_xpath('//*[@id="personal-table"]/tbody/tr[1]/td[2]').text
        #断言
        insert = Report()
        if result == name:
            print('跟踪成功')
            insert.write_report('3.5', '资源管理', 'GUI测试', 'TC-007', '跟踪培训资源', '成功', '无', '无')
        else:
            print('跟踪失败')
            img = insert.pic_screenshot()
            insert.write_report('3.5', '资源管理', 'GUI测试', 'TC-007', '跟踪培训资源', '成功', '无', img)






if __name__ == '__main__':
    start = Add()
    start.update()